package com.example.sunsg.okhttptest.roobo;

import android.text.TextUtils;

public class PasswordUtil {
    private static PasswordUtil instance = null;

    private PasswordUtil() {

    }

    public synchronized static PasswordUtil getInstance() {
        try {
            instance = new PasswordUtil();
        } catch (Exception e) {
            return null;
        }
        return instance;
    }

    public static String getJuanPassword(String pwd) {
        // md5('geG^_s[3Kl' + passwd)
        if (TextUtils.isEmpty(pwd)) {
            return null;
        }
        return MD5Util.getInstance().getStringHash(AppConfig.PASSWORD_PREFIX + pwd);
    }

    public static boolean checkToken(String token, String tm, String mytm, String userId) {
        // md5(';E4`^e@V.g' + md5('=i*KE;6:jh5:N=Ix0~' + 上传的tm值 + 下行的mytm值 + userid))
        if (TextUtils.isEmpty(token)) {
            return false;
        }
        String md5Str = MD5Util.getInstance().getStringHash(AppConfig.TOKEN_PREFIX_SECOND + MD5Util.getInstance().getStringHash(AppConfig.TOKEN_PREFIX_FIRST + tm + mytm + userId));
        return token.equals(md5Str);
    }

    public static boolean checkVCode(String vcode, String input, String phone, int validtm) {
        if (TextUtils.isEmpty(vcode) || TextUtils.isEmpty(input) || TextUtils.isEmpty(phone) || validtm <= 0) {
            return false;
        }
        String raw = phone + input + validtm;
        String targetVCode = MD5Util.md5(AppConfig.VALID_CODE_PREFIX + raw);
        return vcode.equals(targetVCode);
    }
}